package cn.dankal.flyaudio.friend;

import android.content.DialogInterface;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.Switch;

import com.jeremyliao.liveeventbus.LiveEventBus;
import com.kongzue.dialog.v2.SelectDialog;

import org.greenrobot.eventbus.EventBus;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import cn.dankal.basiclib.base.activity.BaseActivity;
import cn.dankal.basiclib.base.recyclerview.OnRvItemClickListener;
import cn.dankal.basiclib.common.bus.LiveDataBusConstants;
import cn.dankal.basiclib.util.JMessageToastUtils;
import cn.dankal.basiclib.util.ToastUtils;
import cn.dankal.flyaudio.FlyApplication;
import cn.dankal.flyaudio.R;
import cn.dankal.flyaudio.adapter.GroupMembersAdapter;
import cn.dankal.flyaudio.database.FriendRecommendEntry;
import cn.dankal.flyaudio.entity.Event;
import cn.dankal.flyaudio.entity.EventType;
import cn.jpush.im.android.api.JMessageClient;
import cn.jpush.im.android.api.callback.DownloadAvatarCallback;
import cn.jpush.im.android.api.callback.GetAvatarBitmapCallback;
import cn.jpush.im.android.api.callback.GetUserInfoCallback;
import cn.jpush.im.android.api.model.Conversation;
import cn.jpush.im.android.api.model.UserInfo;
import cn.jpush.im.api.BasicCallback;

public class PrivateChatInfoActivity extends BaseActivity implements View.OnClickListener {

    private RecyclerView membersView;


    private GroupMembersAdapter adapter;

    private String targetId;
    private List<UserInfo> mMemberInfoList = new ArrayList<>();
    private Switch setTopView;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_private_chat_info;
    }

    @Override
    protected void initComponents() {
        addSingleTitleBar("聊天详情");
        membersView = findViewById(R.id.members_recycler);
        adapter = new GroupMembersAdapter();
        setTopView = findViewById(R.id.switch_top);
        membersView.setLayoutManager(new GridLayoutManager(this, 5));
        membersView.setAdapter(adapter);
        findViewById(R.id.sl_delete_person).setOnClickListener(this);
        adapter.setOnRvItemClickListener(new OnRvItemClickListener<UserInfo>() {
            @Override
            public void onItemClick(View v, int position, UserInfo data) {
                Intent intent = new Intent(PrivateChatInfoActivity.this, PickContractPersonActivity.class);
                intent.putExtra("type", PickContractPersonActivity.TYPE_PRIVATE_CREATE_GROUP);
                intent.putExtra(FlyApplication.TARGET_ID, targetId);
                startActivity(intent);
            }
        });

        initData();
    }

    private void initData() {
        targetId = getIntent().getStringExtra(FlyApplication.TARGET_ID);
        Conversation cov = JMessageClient.getSingleConversation(targetId);
        String extra = cov.getExtra();
        setTopView.setChecked(!TextUtils.isEmpty(extra));
        setTopView.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    cov.updateConversationExtra("1");
                    LiveEventBus.get(LiveDataBusConstants.PRIVATE_MESSAGE_SET_TOP,
                            Conversation.class).post(cov);
                } else {
                    cov.updateConversationExtra("");
                    LiveEventBus.get(LiveDataBusConstants.PRIVATE_MESSAGE_CANCEL_SET_TOP, Conversation.class).post(cov);
                }
            }
        });
        JMessageClient.getUserInfo(targetId, new GetUserInfoCallback() {
            @Override
            public void gotResult(int i, String s, UserInfo userInfo) {
                mMemberInfoList.add(userInfo);
                mMemberInfoList.add(new UserInfo() {
                    @Override
                    public String getNotename() {
                        return null;
                    }

                    @Override
                    public String getNoteText() {
                        return null;
                    }

                    @Override
                    public long getBirthday() {
                        return 0;
                    }

                    @Override
                    public File getAvatarFile() {
                        return null;
                    }

                    @Override
                    public void getAvatarFileAsync(DownloadAvatarCallback downloadAvatarCallback) {

                    }

                    @Override
                    public void getAvatarBitmap(GetAvatarBitmapCallback getAvatarBitmapCallback) {

                    }

                    @Override
                    public File getBigAvatarFile() {
                        return null;
                    }

                    @Override
                    public void getBigAvatarBitmap(GetAvatarBitmapCallback getAvatarBitmapCallback) {

                    }

                    @Override
                    public int getBlacklist() {
                        return 0;
                    }

                    @Override
                    public int getNoDisturb() {
                        return 0;
                    }

                    @Override
                    public boolean isFriend() {
                        return false;
                    }

                    @Override
                    public String getAppKey() {
                        return null;
                    }

                    @Override
                    public void setUserExtras(Map<String, String> map) {

                    }

                    @Override
                    public void setUserExtras(String s, String s1) {

                    }

                    @Override
                    public void setBirthday(long l) {

                    }

                    @Override
                    public void setNoDisturb(int i, BasicCallback basicCallback) {

                    }

                    @Override
                    public void removeFromFriendList(BasicCallback basicCallback) {

                    }

                    @Override
                    public void updateNoteName(String s, BasicCallback basicCallback) {

                    }

                    @Override
                    public void updateNoteText(String s, BasicCallback basicCallback) {

                    }

                    @Override
                    public String getDisplayName() {
                        return null;
                    }
                });
                adapter.addDatas(mMemberInfoList, true);

            }
        });
//        messageSwitch.setChecked(mGroupInfo.isGroupBlocked() == 1);
//        messageSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
//            @Override
//            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
//                mGroupInfo.setBlockGroupMessage(isChecked ? 1 : 0, new BasicCallback() {
//                    @Override
//                    public void gotResult(int i, String s) {
//
//                    }
//                });
//            }
//        });
//        //设置群主头像和名字
//        JMessageClient.getUserInfo(mGroupInfo.getGroupOwner(), new GetUserInfoCallback() {
//            @Override
//            public void gotResult(int i, String s, UserInfo userInfo) {
//                if (i == 0) {
//                    if (userInfo.getAvatarFile() != null) {
////                        mIv_ownerAvatar.setImageBitmap(BitmapFactory.decodeFile(userInfo.getAvatarFile().getAbsolutePath()));
//                    }
////                    mTv_ownerName.setText(userInfo.getDisplayName());
//                }
//            }
//        });
//        mListAdapter = new GroupMemberListAdapter(this, mMemberInfoList, mGroupId);
//        mGroup_listView.setAdapter(mListAdapter);
//
//        mLl_groupSearch.setOnClickListener(bg_card_details_1 -> {
//            Intent intent = new Intent(GroupMemberListActivity.this, SearchGroupActivity.class);
//            intent.setFlags(1);
//            JGApplication.mSearchGroup = null;
//            JGApplication.mSearchGroup = mMemberInfoList;
//            startActivity(intent);
//        });
//
//        mTv_back.setOnClickListener(bg_card_details_1 -> finish());
//
//        mSideBar.setOnTouchingLetterChangedListener(s -> {
//            int position = mListAdapter.getSectionForLetter(s);
//            if (position != -1 && position < mListAdapter.getCount()) {
//                //设置滑动sidebar时listView滑动到指定位置
//                mGroup_listView.setSelection(position);
//            }
//        });
//
//        //点击群成员列表,跳转详情界面
//        mGroup_listView.setOnItemClickListener((parent, view, position, id) -> {
//            Object itemAtPosition = parent.getItemAtPosition(position);
//            if (itemAtPosition instanceof UserInfo) {
//                UserInfo info = (UserInfo) itemAtPosition;
//                Intent intent = new Intent();
//                intent.setClass(GroupMemberListActivity.this, GroupUserInfoActivity.class);
//                intent.putExtra("groupID", mGroupId);
//                intent.putExtra("groupOwner", mGroupInfo.getGroupOwner());
//                intent.putExtra("groupUserName", info.getUserName());
//                startActivity(intent);
//            }
//        });
    }


    @Override
    public void onClick(View v) {
        int id = v.getId();
        switch (id) {
            case R.id.sl_delete_person:
                SelectDialog.show(PrivateChatInfoActivity.this, "提示", "是否要删除", "确定", (dialog, which) -> {
                    mMemberInfoList.get(0).removeFromFriendList(new BasicCallback() {
                        @Override
                        public void gotResult(int status, String s) {
                            if (status == 0) {

                                Conversation conv = JMessageClient.getSingleConversation(targetId);
//                                conv = Conversation.createSingleConversation(targetId);
                                EventBus.getDefault().post(new Event.Builder().setType(EventType.deleteConversation)
                                        .setConversation(conv)
                                        .build());
                                ToastUtils.showShort("已删除好友");
                                LiveEventBus.get("dissolve_frient", String.class).post(mMemberInfoList.get(0).getUserName());

                                FriendRecommendEntry friendEntry =
                                        FriendRecommendEntry.getEntry(FlyApplication.getUserEntry(),
                                                mMemberInfoList.get(0).getUserName(),mMemberInfoList.get(0).getAppKey());
                                friendEntry.delete();

                                JMessageClient.deleteSingleConversation(targetId);
                                dismissLoadingDialog();
                                finishActivity();
                            } else {
                                dismissLoadingDialog();
                                JMessageToastUtils.errorToast(status);
                            }
                        }
                    });
                }, "取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                });

                break;
        }
    }
}
